#define APP_VERSION "0.0.1"

#include <QtGui/QApplication>
#include <QTranslator>
#include <QDir>
#include <QTextCodec>
#include "mainwindow.h"
#include "connectiondialog.h"
#include "changeuserdialog.h"

void loadUserSettings();

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    a.setOrganizationName("GAI");
    a.setApplicationName("AccidentAnalysis");
    a.setApplicationVersion(APP_VERSION);

    loadUserSettings();
    a.setStyleSheet("QTableView { selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0.5, y2: 0.5, stop: 0 #FF92BB, stop: 1 white);}");

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    QTranslator *qt_translator = new QTranslator;
    if (qt_translator->load("qt_ru.qm"))
        a.installTranslator(qt_translator);

    bool connected = false;
    ConnectionDialog connectDialog;
    while(1) {
        if(!QSqlDatabase::database().isOpen()) {
            if (connectDialog.exec() == QDialog::Rejected) {
                connected = false;
                break;
            }
        } else {
            connected = true;
            break;
        }
    }

    if (connected) {
        ChangeUserDialog changeUserDialog;
//        if (changeUserDialog.exec() == QDialog::Rejected)
//            return 0;
        MainWindow *mainWindow = new MainWindow();
        mainWindow->show();
        qApp->setActiveWindow(mainWindow);
    }
    return a.exec();
}

void loadUserSettings()
{
    QSettings settings;
    settings.beginGroup("user settings");
    QString str = settings.value("font").value<QFont>().toString(); //delete
    if (settings.value("font").value<QFont>() != QFont()) {
        qApp->setFont(settings.value("font").value<QFont>());
    }
    settings.endGroup();
}
